import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; // 【新增】
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'app/data/services/background_service.dart';
import 'app/routes/app_pages.dart';
import 'app/main_binding.dart';
import 'core/values/app_styles.dart';
import 'package:flutter_background_service/flutter_background_service.dart'; // 【新增】

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initializeBackgroundService();
  await GetStorage.init();

  // 【核心】设置 MethodChannel 监听
  const channel = MethodChannel('com.example.grab_the_order/bridge');
  channel.setMethodCallHandler((call) async {
    if (call.method == 'dispatchOrderData') {
      final Map<String, dynamic> orderData =
          Map<String, dynamic>.from(call.arguments);
      print("Main Isolate 收到来自原生的订单数据: $orderData");
      // 将数据转发给后台服务
      FlutterBackgroundService().invoke('onOrderDetected', orderData);
    }
  });

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: "抢单助手",
      theme: ThemeData(
        scaffoldBackgroundColor: kBackgroundColor,
        fontFamily: 'Roboto',
        appBarTheme: AppBarTheme(
          backgroundColor: kBackgroundColor,
          elevation: 0,
          centerTitle: true,
          titleTextStyle: TextStyle(
            color: Colors.grey[800],
            fontSize: 20,
            fontWeight: FontWeight.bold,
          ),
          iconTheme: IconThemeData(color: Colors.grey[800]),
        ),
        bottomNavigationBarTheme: const BottomNavigationBarThemeData(
          backgroundColor: Colors.white,
          selectedItemColor: kPrimaryColor,
          unselectedItemColor: Colors.grey,
          elevation: 5,
          selectedLabelStyle: TextStyle(fontWeight: FontWeight.bold),
        ),
      ),
      initialBinding: MainBinding(),
      initialRoute: AppPages.INITIAL,
      getPages: AppPages.routes,
      debugShowCheckedModeBanner: false,
    );
  }
}
